package com.scala.listdemo

import java.util

import scala.collection.mutable

/**
  * Created by Administrator on 2017/7/5.
  */
object ListDemo {


  def sum(list: List[Int]): Int = {
    if (list != Nil) {
      list.head + sum(list.tail)
    } else {
      0
    }
  }

  def sumMatch(list: List[Int]): Int = list match {
    case Nil => 0
    case h :: t => h + sumMatch(t)
  }


  def main(args: Array[String]): Unit = {

    val list = List(0, 2, 3, 4, 5, 6, 7, 8, 9)
    println(list.head)
    println(list.last)
    println(list.headOption)
    println(list.lastOption)
    println(list.tail)
    println(list.init)
    println(list.length)
    println(list.isEmpty)


    val collect = list.collect {
      case 1 => 1
      case 2 => 2
    }
    println(collect)

    val mapList = list.map(x => x * x)

    mapList.foreach(x => print(x + ","))
    println("")


    val iterable = list.iterator
    while (iterable.hasNext) {
      println(iterable.next())
    }

    val sum = list.reduceRight(_ - _)

    println(sum)

    val sum1 = list.foldRight(0)(_ - _)

    println(sum1)

    val reduceSum=list.reduce(_-_)

    println(reduceSum)

    val par=list.par

   val  aggre=par.aggregate((1,0))(
      (acc,number) => {println(acc+","+number);(acc._1*number,acc._2+1)},
      (par1,par2) => {println(par1+","+par2);(par1._1 + par2._1, par1._2 + par2._2)}

    )

    println(aggre)

  }

}
